package com.cn.studey.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class DateUtils {

    public static final String dateFormat = "yyyyMMddHHmm";
    public static final String dateFormat1 = "yyyy-MM-dd HH:mm";
    public static final String dateFormat2 = "yyMMddHH";
    public static final String dateFormat3 = "yyMMddHHmmss";
    public static final String dateFormat4 = "yyyy-MM-dd HH:mm:ss";
    public static final String dateFormat5 = "yyyy-MM-dd";
    public static final String dateFormat6 = "yyyy年MM月dd日 HH:mm";
    public static final String dateFormatOnenet = "yyyy-MM-dd'T'HH:mm:ss";
    public static final String dateFormat7 = "MM月dd日";
    public static final String dateFormat8 = "HH:mm";
    public static final String dateFormat9 = "HH:mm:ss";
    public static final String dateFormat10 = "yyyy:MM:dd";
    public static final String dateFormat11 = "yyyyMMdd HH:mm:ss";
    public static final String dateFormat12 = "yyyy年MM月dd日";
    public static final String dateFormat13 = "yyyy-MM";
    public static final String dateFormat14 = "HHmmss";
    public static final String dateFormat15 = "yyyy-MM-dd HH:mm";
    public static final String dateFormat16 = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String dateFormat17 = "yyyy.MM.dd.HH.mm.ss";


    private static final ThreadLocal<Map<String, DateFormat>> formatThreadLocal = ThreadLocal.withInitial(HashMap::new);

    /**
     * 为每个线程创建本地变量
     *
     * @param dateFormat string
     * @return DateFormat
     */
    private static DateFormat getDateFormat(String dateFormat) {
        Map<String, DateFormat> formatters = formatThreadLocal.get();
        DateFormat format = formatters.get(dateFormat);
        if (format == null) {
            format = new SimpleDateFormat(dateFormat);
            formatters.put(dateFormat, format);
        }
        return format;
    }

    public static Date parse(String dateStr, String dateFormat) throws ParseException {
        return getDateFormat(dateFormat).parse(dateStr);
    }

    public static String format(Date date, String dateFormat) {
        return getDateFormat(dateFormat).format(date);
    }

    public static int compareHMSTime(String timeStr1, String timeStr2) {//大于等于返回1，小于返回0，非法也返回0
        int compareResult = 0;
        try {
            if (timeStr1.indexOf(":") < 0 || timeStr1.indexOf(":") < 0) {
                System.out.println("格式不正确");
            } else {
                String[] array1 = timeStr1.split(":");
                int total1 = Integer.valueOf(array1[0]) * 3600 + Integer.valueOf(array1[1]) * 60 + Integer.valueOf(array1[2]);
                String[] array2 = timeStr2.split(":");
                int total2 = Integer.valueOf(array2[0]) * 3600 + Integer.valueOf(array2[1]) * 60 + Integer.valueOf(array2[2]);
                compareResult = total1 - total2 >= 0 ? 1 : 0;
            }
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            compareResult = 0;
        }
        return compareResult;
    }

    //判断两个日期是否是同一天
    public static boolean isSameDate(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
                .get(Calendar.YEAR);
        boolean isSameMonth = isSameYear
                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
        boolean isSameDate = isSameMonth
                && cal1.get(Calendar.DAY_OF_MONTH) == cal2
                .get(Calendar.DAY_OF_MONTH);
        return isSameDate;
    }

    //获取当前时间戳月份的结束时间戳
    public static Long getEndTimeInMonth(Long time) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        Date date = new Date(time);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
        String format = simpleDateFormat.format(date);
        Date parse = simpleDateFormat.parse(format);
        calendar.setTime(parse);
        calendar.add(Calendar.MONTH, 1);
        Date time1 = calendar.getTime();
        return time1.getTime();
    }

    public static Date getDayBeginTime(Date date) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(date);
        Date parse = simpleDateFormat.parse(format);
        return parse;
    }

    public static Date getAppointDay(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, days);
        return calendar.getTime();
    }

    public static Date getAppointMonth(Date date, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, month);
        return calendar.getTime();
    }

    public static Date getYearBeginTime(Date date) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
        String format = simpleDateFormat.format(date);
        Date parse = simpleDateFormat.parse(format);
        return parse;
    }

    public static Long getTimestamp(String dateFormat4, String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat4);
        try {
            Date parse = simpleDateFormat.parse(date);
            return parse.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0L;
    }

    public static String getDateStr(long timestamp, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(new Date(timestamp));
    }
    public static Date getDateByMonth(int month,Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(calendar.MONTH, month);
        return calendar.getTime();
    }
}
